package com.news.servlet;

import com.news.common.BaseServlet;
import com.news.common.CommonPage;
import com.news.common.CommonResult;
import com.news.common.ServletResp;
import com.news.dao.impl.CmsCommentDaoImpl;
import com.news.entity.Article;
import com.news.entity.Comment;
import com.news.enums.Role;
import com.news.utils.JsonUtils;
import sun.plugin2.gluegen.runtime.CPU;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/4/29
 * 评论模块
 */
@WebServlet("/comment/*")
public class CommentServlet extends BaseServlet {

    /**
     * （客户端）获取文章评论
     * @param request
     * @param response
     */
    public void getComments(HttpServletRequest request, HttpServletResponse response){
        String idStr = request.getParameter("articleId");
        Integer articleId = Integer.valueOf(idStr);
        List<Comment> commentList = new CmsCommentDaoImpl().getComments(articleId);
        if (commentList!=null){
            ServletResp.resp(response, CommonPage.success(commentList));
        }else {
            ServletResp.resp(response, CommonResult.failed());
        }
    }


    /**
     * （客户端）添加文章评论
     * @param request
     * @param response
     */
    public void addComment(HttpServletRequest request, HttpServletResponse response){
        String reqBody = JsonUtils.getJson(request);
        Comment comment = JsonUtils.fromJson(reqBody, Comment.class);
        String userName = (String) request.getSession().getAttribute("userName");
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Integer role = (Integer) request.getSession().getAttribute("role");
        if (userId == null || Role.Admin.getRoleCode()==role){
            ServletResp.resp(response, CommonResult.unauthorized());
        }
        comment.setUsername(userName);
        comment.setUserId(userId);
        int result = new CmsCommentDaoImpl().doComment(comment);
        if (result == 1){
            ServletResp.resp(response, CommonResult.success("评论成功，审核通过后显示"));
        }else {
            ServletResp.resp(response, CommonResult.failed("评论失败"));
        }
    }

    /**
     * （管理端）获取等待审核的评论
     * @param request
     * @param response
     */
    public void getCommentWaitCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer role = (Integer) request.getSession().getAttribute("role");
        if (Role.Admin.getRoleCode()!=role){
            response.sendRedirect("admin?method=toLogin");
        }
        String pageNumStr = request.getParameter("page");
        String pageSizeStr = request.getParameter("limit");
        if (pageNumStr == null || pageSizeStr == null){
            ServletResp.resp(response, CommonResult.validateFailed());
        }
        int pageSize = Integer.valueOf(pageSizeStr);
        int pageNum = Integer.valueOf(pageNumStr);
        pageNum = (pageNum-1) * pageSize;
        List<Map<String,Object>> allComments = new CmsCommentDaoImpl().getCommentWaitCheck(pageNum,pageSize);
        if (allComments!=null){
            ServletResp.resp(response, CommonPage.success(allComments));
        }else{
            ServletResp.resp(response, CommonResult.failed());
        }

    }

    /**
     * 更新评论
     * @param request
     * @param response
     */
    public void updateComment(HttpServletRequest request, HttpServletResponse response){
        String reqBody = JsonUtils.getJson(request);
        Comment comment = JsonUtils.fromJson(reqBody, Comment.class);
        int result = new CmsCommentDaoImpl().updateComment(comment);
        if (result == 1){
            ServletResp.resp(response, CommonResult.success("修改评论状态成功"));
        }else {
            ServletResp.resp(response, CommonResult.failed());
        }
    }

}
